package com.yanqu.road.server.cmd.monopolymarket.res;

import com.yanqu.road.entity.enums.eMonopolyMarketResType;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.entity.monopolymarket.config.MonopolyMarketResConfigData;
import com.yanqu.road.entity.monopolymarket.resgrab.MonopolyMarketResGrabMap;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.monopolymarket.MonopolyMarketProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.monopolymarket.MonopolyMarketModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.MonopolyMarketCommonMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;
import java.util.*;

/**
 * @Description
 * @Author cwq
 * @Data 2021/8/11 16:32
 */
@Cmd(code = GameProtocol.S_MONOPOLY_MARKET_RES_GRAB_ACTION, desc = "资源争夺战 撤销/抢夺 位置")
public class MonopolyMarketResGrabActionCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        MonopolyMarketProto.MonopolyMarketResGrabActionReqMsg req = MonopolyMarketProto.MonopolyMarketResGrabActionReqMsg.parseFrom(packet.getBytes());
        if(!MonopolyMarketMgr.canEnterSystem(player)){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        if(!MonopolyMarketCommonMgr.isSystemOpen(MonopolyMarketMgr.group, req.getType())){
            ret = GameErrorCode.E_MONOPOLY_MARKET_SYSTEM_NO_OPEN;
            return;
        }
        if(req.getType() != eMonopolyMarketResType.RES_KING.getValue() && req.getType() != eMonopolyMarketResType.RES_QUEEN.getValue()){
            ret = GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_TYPE_ERROR;
            return;
        }
        // 刷新玩家
        MonopolyMarketMgr.refreshUser(player,true,false);
        MonopolyMarketModule marketModule = player.getModule(MonopolyMarketModule.class);
        MonopolyMarketProto.MonopolyMarketResGrabActionCrossReqMsg.Builder toCrossMsg = MonopolyMarketProto.MonopolyMarketResGrabActionCrossReqMsg.newBuilder();
        if(req.getActionType() == 0){
            // 入座
            // 判断体力
            ret = MonopolyMarketMgr.checkBuffTimes(req.getType(), req.getBuffAddTimes());
            if(ret != 0){
                return;
            }
            MonopolyMarketResGrabMap marketResGrabMap = MonopolyMarketMgr.resGrabMapMap.get(req.getType());
            if(marketResGrabMap == null){
                ret = GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_NO_MAP;
                return;
            }
            if(marketResGrabMap.indexLevelList.size() <= req.getPosIndex() ){
                ret = GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_POINT_NO_EXIST;
                return ;
            }
            Integer level = marketResGrabMap.indexLevelList.get(req.getPosIndex());
            if(level == 0){
                ret = GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_POINT_NO_EXIST;
                return;
            }
            Map<Integer, MonopolyMarketResConfigData> resConfigDataMap = MonopolyMarketMgr.resConfigLevelMap.get(req.getType());
            if(resConfigDataMap == null){
                ret = GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_CONFIG_NO_EXIST;
                return;
            }
            MonopolyMarketResConfigData resConfigData = resConfigDataMap.get(level);
            if(resConfigData == null){
                ret = GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_CONFIG_NO_EXIST;
                return;
            }
            if(req.getObjectIdCount() <= 0 ){
                ret = GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_NO_OBJECT;
                return;
            }
            // 派遣门客上限
            ret = MonopolyMarketMgr.checkDispatchNum(req.getType(), req.getObjectIdCount());
            if(ret != 0){
                return;
            }
            // 派遣队伍上限  跨服上判断
            ret = MonopolyMarketMgr.checkTeamNum(player,req.getType());
            if(ret != 0){
                return;
            }
            // 判断体力
            if(marketModule.baseData.getPhysical() < resConfigData.getCost()){
                ret = GameErrorCode.E_MONOPOLY_MARKET_NO_PHYSICAL;
                return;
            }
            // buff加成需要的元宝数量
            int needIngots = 0;
            Set<Integer> tempObjectSet = new HashSet<>();
            List<UserPatrons> patronsList = new ArrayList<>();
            List<UserBeauty> beautyList = new ArrayList<>();
            for(Integer objectId : req.getObjectIdList()){
                if(marketModule.isDispatch(req.getType(),objectId)){
                    // 派遣中
                    ret = GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_DISPATCH_OBJECT_REPEATED;
                    return;
                }
                if(tempObjectSet.contains(objectId)){
                    ret = GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_OBJECT_ID_IS_DUPLICATE;
                    return;
                }
                tempObjectSet.add(objectId);

                if(req.getType() == eMonopolyMarketResType.RES_KING.getValue()){
                    UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(objectId);
                    if(userPatrons == null){
                        ret = GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_OBJECT_NOT_EXIST;
                        return;
                    }
                    patronsList.add(userPatrons);
                }else if(req.getType() == eMonopolyMarketResType.RES_QUEEN.getValue()){
                    UserBeauty userBeauty = player.getModule(BeautyModule.class).getUserBeauty(objectId);
                    if (userBeauty == null) {
                        ret = GameErrorCode.E_BEAUTY_NO_FOUND;
                        return;
                    }
                    beautyList.add(userBeauty);
                }
            }

            if(req.getType() == eMonopolyMarketResType.RES_KING.getValue()){
                // 判断门客是否存在
                for (UserPatrons item : patronsList){
                    // 门客数据 to cross
                    toCrossMsg.addPatronsList(MonopolyMarketMgr.builderMonopolyMarketPatronsEntity(player, item, patronsList));
                }
            }else if (req.getType() == eMonopolyMarketResType.RES_QUEEN.getValue()){
                int addPlus = PatronsMgr.getMonopolyMarketPatronsPlus(player, eSkillType.CALABASH_BROTHERS_QUEEN.getValue());
                for (UserBeauty beauty : beautyList){
                    // 美女数据 to cross
                    toCrossMsg.addBeautyList(MonopolyMarketMgr.builderMonopolyMarketBeautyEntity(beauty,addPlus));
                }
            }

            needIngots = MonopolyMarketMgr.calBuffIngots(req.getType(),req.getBuffAddTimes());
            if(needIngots > 0){
                // 判断元宝
                Property property = new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(needIngots));
                if(!player.getModule(CurrencyModule.class).currencyIsEnough(property)){
                    ret = GameErrorCode.E_BAG_INGOTS_NO_ENOUGH;
                    return;
                }
                marketModule.resGrabNeedConsumeGold = needIngots;
                // 预扣除
                player.getModule(PreReduceModule.class).preReduce(property);
            }
            toCrossMsg.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
            marketModule.resGrabNeedPhysical = resConfigData.getCost();
            marketModule.level = resConfigData.getLevel();
        }else {
            // 撤位置
        }
        toCrossMsg.setCtsMsg(req);
        player.sendPacket(CrossProtocol.C_CROSS_MONOPOLY_MARKET_RES_GRAB_ACTION, toCrossMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            MonopolyMarketProto.MonopolyMarketResGrabActionRespMsg.Builder builder = MonopolyMarketProto.MonopolyMarketResGrabActionRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_MONOPOLY_MARKET_RES_GRAB_ACTION, builder);
        }
    }

}
